import os
import sys
import time
import thread
from paramsetup import Param
from seleniumsetup import SeleniumSetup
from executer import Executer



#cmd = "java -jar " + sys.path[0] + '\\selenium\\selenium-server-standalone-2.0.0.jar'
#print 'starting selenium server...'
#
#thread.start_new_thread(os.system(cmd))
#print 'cmd over...'
#time.sleep(5000)
#print 'sleep over...'


def getTimeTag():
    tag = ""
    t = time.gmtime()
    for i in range(6):
        tag = tag+str(t[i])   
    return tag    

def addLibPath():
    exe_dir = sys.path[0]
    sys.path.append(exe_dir + sys.path[0] + '\\lib\\matchIt.jar')
    
#1.
#dateTag is used as a tag in the test log name
#used to distinguish different test log

timeTag = getTimeTag()  

#2.
#test envirionment has several params
#used for selenium, test log and test report

buildVer = OS = browsers = logId = ""
url = buildDir = ""

f = open(sys.path[0] + '\\test_env.txt','r')

for line in f.readlines():

    name  = line.split('=')[0].strip()
    value = line.split('=')[1].strip()
            
    if name == 'buildVer':
        buildVer   = value
    elif name == 'logId':
        logId     = timeTag
    elif name == 'OS':
        OS              = value
    elif name == 'browsers':
        browsers        = value.split(',')
    elif name == 'url':
        url             = value
    elif name == 'buildDir':
        buildDir = value
    elif name == 'testcasefolder':
        testcasefolder = value
        
#print buildVer, logId,OS,browsers,url,buildDir
#3.
#MatchIt.jar is for image comparision, will be used in the Writer.py
#as an environment Param, we need to add it into sys.path previousely

addLibPath()
  
for i in range(len(browsers)):        
    SeleniumSetup.setup(OS,browsers[i],url,buildDir,logId)
    SeleniumSetup.user.window_focus()
    
    Param.user      = SeleniumSetup.user
    Param.buildDir  = buildDir
    Param.OS        = OS
    Param.browser   = browsers[i]
    Param.browser   = browsers[i]
    Param.logId     = logId    
    Param.testcasefolder = testcasefolder
    
    Executer.run()
    
    SeleniumSetup.user.close()
    time.sleep(5)